



package wviz.gui.graph;

import prefuse.Display;
import prefuse.Visualization;
import prefuse.visual.VisualItem;
import prefuse.controls.ControlAdapter;
import prefuse.data.Graph;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.MouseEvent;


public abstract class BaseDisplay extends Display implements ChangeListener {

    protected final String dataGroup;
    protected final String filterLabel;
    protected final int filterMaxValue;
    private final String searchGroup;

    public BaseDisplay(Visualization visualization, String dataGroup, int filterMaxValue, String filterLabel, String searchGroup) {
        super(visualization);
        this.dataGroup = dataGroup;
        this.filterLabel = filterLabel;
        this.filterMaxValue = filterMaxValue;
        this.searchGroup = searchGroup;

        //add mouse click listener for notifying change listeners
        addControlListener(new ControlAdapter(){
            @Override
            public void itemClicked(VisualItem item, MouseEvent e) {
                if (item.canGetString(GraphGenerator.WORD_COLUMN)) {
                    Object value = item.get(GraphGenerator.WORD_COLUMN);
                    //notify all listeners for value of this item
                    firePropertyChange(GraphGenerator.WORD_COLUMN, null, value);
                }
            }
        });
    }

    public String getDataGroup() {
        return dataGroup;
    }

    public String getFilterLabel() {
        return filterLabel;
    }

    public int getFilterMaxValue() {
        return filterMaxValue;
    }

    public String getSearchGroup() {
        return searchGroup;
    }

    public void stateChanged(ChangeEvent e) {
        int value = ((ValueSlider) e.getSource()).getValue().intValue();
        value = filterMaxValue - value;
        this.setFilterLevel(value);
    }

    public abstract void setFilterLevel(int level);

    /**
     * Implementations of this method will create actual display nodes, add colours and so
     * @param g the graph contains data nodes
     */
    public abstract void setupGraph(Graph g);


}